package mlp.game.shadownbolts;

import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PointF;

public class Shadowbolt extends Renderable {
	private float invert [] = { 
	        -1.0f,  0.0f,  0.0f,  1.0f,  0.0f, 
	        0.0f,  -1.0f,  0.0f,  1.0f,  0.0f, 
	        0.0f,  0.0f,  -1.0f,  1.0f,  0.0f, 
	        1.0f,  1.0f,  1.0f,  1.0f,  0.0f 
	};
	
	
	private int movementSpeedPx = 5;
	private int movementDelayMs = 10;
	private long lastTimeMoved = 0;

	public int hp = 100;
	public boolean useHitPaint = false;
	
	private float baseX;
	private float spawnX;
	private int animationCounter = 0;
	private boolean spawned = false;
	
	private boolean canFire;
	
	private long nextRandomFire = 0;
	
	private Paint paint;
	
	public boolean canFire() {
		return canFire;
	}
	
	public void clearCanFire(){
		canFire = false;
	}

	public Shadowbolt(Sprite sprite){
		super(sprite);
		this.paint = new Paint();
		ColorMatrix cm = new ColorMatrix(invert); 
		paint.setColorFilter(new ColorMatrixColorFilter(cm)); 
		this.baseX = RenderView.visibleScreenArea.width() - getSprite().getWidth() - 50;
		this.spawnX = RenderView.visibleScreenArea.width();
		setPosition(new PointF(spawnX, RenderView.visibleScreenArea.centerY()));
	}
	
	public void spawn(){
		this.hp = 100;
		this.animationCounter = 60;
		spawned = false;
		moveX(spawnX);
	}
	
	public void takeHit(){
		this.hp -= 10;
	}
	
	public void update(long gameTime){
		if(this.animationCounter > 0){
			if(animationCounter < 30){
				this.moveX(getPosition().x + (baseX - getPosition().x) / 10);
			}			
			animationCounter--;
		}
		if(animationCounter <= 0){
			this.spawned = true;
		}
		if(gameTime - lastTimeMoved > movementDelayMs){
			if((getFrame().bottom + movementSpeedPx) > RenderView.visibleScreenArea.height() || (getFrame().top + movementSpeedPx) < 0){
				movementSpeedPx = -movementSpeedPx;
			}
			super.moveBy(0, movementSpeedPx);
			lastTimeMoved = gameTime;
		}
		this.checkToFire(gameTime);
		super.update(gameTime);
	}
	
	public void checkToFire(long gameTime){
		if(nextRandomFire < gameTime && this.spawned){
			nextRandomFire = (long) (1000 + gameTime + Math.random() * 2000);
			canFire = true;
		}
	}
	
	@Override
	public void draw(Canvas canvas) {
		if(useHitPaint){
			super.draw(canvas, paint);
			useHitPaint = false;
		} else {
			super.draw(canvas);			
		}
	}
	
}
